package com.netease.nie.base.utils;

import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;  
import javax.crypto.Cipher;  
import javax.crypto.IllegalBlockSizeException;  
import javax.crypto.NoSuchPaddingException;  
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class DES3Utils {
    private SecretKey deskey;  
    private Cipher c;  
    private byte[] cipherByte;  
  
    public DES3Utils() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException {  
    	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
    	byte[] keyData = ("bicredit12345678bicredit").getBytes();
    	DESedeKeySpec keySpec=new DESedeKeySpec(keyData); 
    	deskey = keyFactory.generateSecret(keySpec);
        c = Cipher.getInstance("DESede");  
    }  
  
 
    public byte[] Encrytor(String str) throws InvalidKeyException,  
            IllegalBlockSizeException, BadPaddingException {  
        c.init(Cipher.ENCRYPT_MODE, deskey);  
        byte[] src = str.getBytes();  
        cipherByte = c.doFinal(src);  
        return cipherByte;  
    }  
  
    public byte[] Decryptor(byte[] buff) throws InvalidKeyException,  
            IllegalBlockSizeException, BadPaddingException {  
        c.init(Cipher.DECRYPT_MODE, deskey);  
        cipherByte = c.doFinal(buff);  
        return cipherByte;  
    }  
    
}
